package com.bieyang.anjia.ui.activity;

import java.util.Iterator;
import java.util.TreeSet;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bieyang.anjia.R;
import com.bieyang.anjia.module.Device;
import com.bieyang.anjia.net.NetManager;
import com.bieyang.anjia.utils.Constants;
import com.bieyang.anjia.utils.SharedPrefsUtil;
import com.bieyang.anjia.utils.Show;
import com.bieyang.anjia.utils.SubUtils;

/**
 * 预警措施
 * 
 * @author M.y.q
 * 
 */
public class AlarmMeasuresActivity extends BaseNomalActivity implements
		OnClickListener {
	RelativeLayout body, dys, flash;
	CheckBox bodyRecCheck, dysCheck, flashLightCheck;
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Constants.SET_SUB_SET_SUCCESS:
				progressDialog.dismiss();
				Log.d("saveSetting", "success");
				SharedPrefsUtil
						.putValue(
								AlarmMeasuresActivity.this,
								Constants.ALARM_MEASURES,
								bodyRecCheck.isChecked() ? getString(R.string.sub_body_recg)
										: getString(R.string.sub_dys));
				SharedPrefsUtil.putValue(AlarmMeasuresActivity.this,
						Constants.FLASH_LIGHT, flashLightCheck.isChecked());
				setResult(2002);
				Show.toast(con, R.string.save_device_ok);
				finish();
				break;
			case Constants.SET_SUB_SET_FAILED:
				progressDialog.dismiss();
				Show.toast(con, R.string.save_device_fail);
				finish();
				break;

			default:
				break;
			}
		};
	};

	@Override
	void setContent() {
		setContentView(R.layout.alarm_measure_layout);
	}

	@Override
	void initView() {
		body = (RelativeLayout) findViewById(R.id.body_rela);
		dys = (RelativeLayout) findViewById(R.id.dys_rela);
		flash = (RelativeLayout) findViewById(R.id.flash_rela);
		TextView titleName = (TextView) findViewById(R.id.topLineTitle);
		titleName.setText(getString(R.string.sub_alarm_measure));
		bodyRecCheck = (CheckBox) findViewById(R.id.body_rec_check);
		dysCheck = (CheckBox) findViewById(R.id.sub_dys_check);
		flashLightCheck = (CheckBox) findViewById(R.id.flash_light_check);
	}

	@Override
	void initData() {
		String measures = SharedPrefsUtil.getStringValue(
				AlarmMeasuresActivity.this, Constants.ALARM_MEASURES,
				getString(R.string.sub_body_recg));
		boolean measure = measures.equals(getString(R.string.sub_body_recg)) ? true
				: false;
		bodyRecCheck.setChecked(measure);
		dysCheck.setChecked(!measure);
		flashLightCheck.setChecked(SharedPrefsUtil.getBooleanValue(
				AlarmMeasuresActivity.this, Constants.FLASH_LIGHT, true));
	}

	@Override
	void setListener() {
		body.setOnClickListener(this);
		dys.setOnClickListener(this);
		flash.setOnClickListener(this);
	}

	protected void saveStatus() {
		super.saveStatus();
		if (!isSetted) {
			finish();
			return;
		}
		upLoadDeviceSet(handler);
	}

	@Override
	public void onClick(View v) {
		isSetted = true;
		switch (v.getId()) {
		case R.id.body_rela:
			bodyRecCheck.setChecked(!bodyRecCheck.isChecked());
			dysCheck.setChecked(!bodyRecCheck.isChecked());
			break;
		case R.id.dys_rela:
			dysCheck.setChecked(!dysCheck.isChecked());
			bodyRecCheck.setChecked(!dysCheck.isChecked());
			break;
		case R.id.flash_rela:
			flashLightCheck.setChecked(!flashLightCheck.isChecked());
			break;
		default:
			break;
		}
	}

	/**
	 * 上传采集端机设置 从本地取得设置信息后给device赋值并上传至服务器
	 * */
	public void upLoadDeviceSet(Handler handler) {
		Log.d("subonline", NetManager.subOnLineMap.get(uuid).toString());
		if (App.isLogin) {
			if (!NetManager.subOnLineMap.get(uuid).toString().equals("ONLINE")) {
				Show.toast(con, R.string.sub_off_line);
				return;
			}
		} else {
			Show.toast(con, R.string.please_login);
			return;
		}
		if (null == progressDialog) {
			progressDialog = SubUtils.showProgress(con);
		} else {
			progressDialog.show();
		}
		Device device = new Device();
		device.setDiviceId(id);
		device.setUuid(uuid);
		String dayStart = SharedPrefsUtil.getStringValue(con,
				Constants.DAY_MODE_START_TIME,
				getString(R.string.sub_mode_start_time));
		String dayStop = SharedPrefsUtil.getStringValue(con,
				Constants.DAY_MODE_STOP_TIME,
				getString(R.string.sub_mode_stop_time));
		String mesures = bodyRecCheck.isChecked() ? getString(R.string.sub_body_recg)
				: getString(R.string.sub_dys);
		String alarm = SharedPrefsUtil.getStringValue(con,
				Constants.AlARM_METHODS, getString((R.string.sub_take_one)));
		String voice = SharedPrefsUtil.getStringValue(con,
				Constants.AlARM_PLAY_VOICE,
				getString((R.string.sub_states_null)));
		String nightStart = SharedPrefsUtil.getStringValue(con,
				Constants.NIGHT_MODE_START_TIME,
				getString(R.string.sub_mode_stop_time));
		String nightStop = SharedPrefsUtil.getStringValue(con,
				Constants.NIGHT_MODE_STOP_TIME,
				getString(R.string.sub_mode_start_time));
		boolean flashLight = SharedPrefsUtil.getBooleanValue(con,
				Constants.FLASH_LIGHT, flashLightCheck.isChecked());
		TreeSet<Integer> chooseSet = subUtils.getWeekDaySet(
				con,
				SharedPrefsUtil.getSetValue(con, Constants.WEEK_DAY,
						subUtils.initWeekSet(con)));

		device.setStartTWhite(dayStart);
		device.setEndTWhite(dayStop);
		device.setDeterrenceVWhite(voice);
		device.setLarmWhite(alarm);
		device.setStartTNight(nightStart);
		device.setEndTNight(nightStop);
		device.setLarmNight(alarm);
		device.setDeterrenceVNight(voice);
		device.setHumanR(mesures.equals(getString(R.string.sub_body_recg)));
		device.setMotionD(!mesures.equals(getString(R.string.sub_body_recg)));
		device.setFlashlight(flashLight);
		device.setWeekWhite(getWeekStr(chooseSet));
		device.setWeekNight(getWeekStr(chooseSet));
		App.netManager.setCollectionTerminal(device, handler);
	}

	/**
	 * 将TreeSet 变为字符串1,2,3,...等上传
	 * */
	private String getWeekStr(TreeSet<Integer> chooseSet) {
		StringBuffer sb = new StringBuffer("");
		Iterator<Integer> it = chooseSet.iterator();
		while (it.hasNext()) {
			int day = it.next();
			sb.append(day);
			sb.append(",");
		}
		String week = sb.toString();
		return week.substring(0, week.length() - 1);
	}
}
